package com.hebeu.device.service.impl;

import com.hebeu.device.dao.DServiceDeviceMapper;
import com.hebeu.device.pojo.DServiceDevice;
import com.hebeu.device.pojo.DServiceDeviceExample;
import com.hebeu.device.pojo.view.ViewService;
import com.hebeu.device.service.ServiceDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author 言立慧
 * @Date 2019/6/19 11:29
 */
@Service
public class ServiceDeviceServiceImpl implements ServiceDeviceService {
    private DServiceDeviceMapper serviceDeviceMapper;

    @Autowired
    public ServiceDeviceServiceImpl(DServiceDeviceMapper serviceDeviceMapper) {
        this.serviceDeviceMapper = serviceDeviceMapper;
    }

    @Override
    public List<DServiceDevice> selectAllDevice() {
        List<DServiceDevice> dServiceDevices = serviceDeviceMapper.selectByExample(new DServiceDeviceExample());
        return dServiceDevices;
    }

    @Override
    public void addServiceDevice(DServiceDevice dServiceDevice) {
        serviceDeviceMapper.insertSelective(dServiceDevice);
    }

    @Override
    public void updateServiceDevice(DServiceDevice dServiceDevice) {
        DServiceDeviceExample  example = new DServiceDeviceExample();
        DServiceDeviceExample.Criteria criteria = example.createCriteria();

        serviceDeviceMapper.updateByPrimaryKeySelective(dServiceDevice);
    }

    @Override
    public DServiceDevice setDevice(Integer sId, ViewService viewService) {
        DServiceDevice device = getServiceDeviceByServiceId(sId);

        DServiceDevice dServiceDevice = new DServiceDevice();
        dServiceDevice.setAlterTime(new Date());
        dServiceDevice.setApperance(viewService.getApperance());
        dServiceDevice.setCreateTime(new Date());
        dServiceDevice.setDevBrand(viewService.getDevBrand());
        dServiceDevice.setDevMix(viewService.getDevMix());
        dServiceDevice.setDevType(viewService.getDevType());
        dServiceDevice.setFaultDepression(viewService.getFaultDepression());
        dServiceDevice.setFaultType(viewService.getFaultType());
        dServiceDevice.setsId(sId);
        dServiceDevice.setDevId(device.getDevId());
        dServiceDevice.setLackPart(viewService.getLackPart());
        dServiceDevice.setPassword(viewService.getPassword());
        dServiceDevice.setSeries(viewService.getSeries());
        return dServiceDevice;
    }

    @Override
    public DServiceDevice getServiceDeviceByServiceId(Integer serviceId) {
        DServiceDeviceExample dServiceDeviceExample = new DServiceDeviceExample();
        DServiceDeviceExample.Criteria criteria = dServiceDeviceExample.createCriteria();
        criteria.andSIdEqualTo(serviceId);
        List<DServiceDevice> dServiceDevices = serviceDeviceMapper.selectByExample(dServiceDeviceExample);
        return dServiceDevices.get(0);
    }
}
